package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.SceneVO;
import com.iwhalecloud.bss.kite.client.vo.spec.ServiceOfferVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.batch.BatchAcceptUtil;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.util.CommonInfoUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.CommonAttrModuleHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
* @Description: TODO
*
* @author lyx
* @date 2020年12月22日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module
*/
@Service
public class CommonInfoOperationModleHandler extends CommonAttrModuleHandler {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public CommonAttrModuleVO dealExecuteSpecialAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        this.removeCommonAttr(context, sceneModule, defCommonAttrInfoVO);
        this.handlerIsEdit(context, sceneModule, defCommonAttrInfoVO);
        return defCommonAttrInfoVO;
    }

    @Override
    public CommonAttrModuleVO dealPreviewSpecialAttr(AcceptContext context, SceneModule sceneModule,
        CommonAttrModuleVO defCommonAttrInfoVO) {
        this.removeCommonAttr(context, sceneModule, defCommonAttrInfoVO);
        return defCommonAttrInfoVO;
    }

    private CommonAttrModuleVO removeCommonAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId());
        KiteOfferInst offerInst = querySceneInstService.getOfferInst(context.getAcceptRequest().getCustId(), kiteSceneInst.getRootInstId());
        String opreatorProvinceNbr= ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String cateId = "";
        if (!KiteObjectUtils.isEmpty(offerInst)) {
            cateId = offerInst.getAttrValue("cateId");
        }
        if (KiteStringUtils.equals(cateId, "2000966")) {
            defCommonAttrInfoVO.getCommonAttrList().removeIf(commAttr -> KiteStringUtils.equals(commAttr.getFieldName(), "CustManagerInfo"));
        }
        if (KiteStringUtils.equals(cateId, "2100949")) {
            defCommonAttrInfoVO.getCommonAttrList().removeIf(commAttr -> KiteStringUtils.equalsAny(commAttr.getFieldName(), "AgentInfo", "CustManagerInfo"));
        }

        // 互联网付费关系模块变更 仅经办人信息可修改，其他不可编辑。
        if (KiteStringUtils.equals(cateId, ProdCateUtil.getSingleCateId(ProdCateUtil.DIA_CODE)) && KiteStringUtils.equals(kiteSceneInst.getServiceOfferId(), "1069")) {
            defCommonAttrInfoVO.getCommonAttrList().stream().forEach(attrInfo -> {
                if(KiteStringUtils.equals(attrInfo.getFieldName(), "AgentInfo")){
                    attrInfo.setIsEdit("F");
                }
            });
        }

        // 河北:18 个性化需求 五大双线屏蔽代理人
        if (KiteStringUtils.isNotEmpty(opreatorProvinceNbr) && KiteStringUtils.equals(opreatorProvinceNbr,"18")) {
            if(KiteStringUtils.equalsAny(cateId, ProdCateUtil.getDoubleLineCateIdForArr())){
                defCommonAttrInfoVO.getCommonAttrList().removeIf(commAttr -> KiteStringUtils.equalsAny(commAttr.getFieldName(), "AgentInfo"));
            }
        }

        // 批量订购、批量签订 经办人必填不可编辑
        if (KiteStringUtils.equals(KeyConsts.IFTRUE_T,
            querySceneInstService.getExtParameterValue(context.getSceneVo().getSceneInstId(), "isBatchAccept"))
            || KiteStringUtils.equals(
                querySceneInstService.getExtParameterValue(context.getSceneVo().getSceneInstId(), "preTaskCode"),
                "batchSIGN")) {
            defCommonAttrInfoVO.getCommonAttrList().forEach(attrInfo -> {
                if (KiteStringUtils.equals(attrInfo.getFieldName(), "HandlerCustInfo")) {
                    attrInfo.setValue("T");
                    attrInfo.setIsEdit("F");
                }
            });
        }


        //在操作栏处理类中设置公共信息的orderId
        SceneVO sceneVO = (SceneVO) context.getSceneVo();
        Map<String, Object> defCommonAttr = sceneVO.getCommonInfo();
        if (KiteMapUtils.isNotEmpty(defCommonAttr)) {
            defCommonAttr.put("orderId", sceneModule.getKiteModule().getOrderId());
        }
        return defCommonAttrInfoVO;
    }

    private CommonAttrModuleVO handlerIsEdit(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        // 物联网运营类变更除经办人信息外的公共信息置灰
        ServiceOfferVO serviceOfferVO = CommonInfoUtils.getServiceOfferVO(context.getAcceptRequest().getSceneInstId(), context.getAcceptRequest().getCustId(), context.getAcceptRequest().getOfferId());
        if (Objects.nonNull(serviceOfferVO) && KeyConsts.INTERNET_THINGS_BUSINESS.equals(serviceOfferVO.getOperateType())) {
            defCommonAttrInfoVO.getCommonAttrList().stream().forEach(attrInfo -> {
                if(!KiteStringUtils.equals(attrInfo.getFieldName(), "HandlerCustInfo")){
                    attrInfo.setIsEdit("F");
                }
            });
        }
        // 批量受理部分环节，不允许编辑
        if (BatchAcceptUtil.isBatchNode(context.getAcceptRequest().getSceneInstId())) {
            defCommonAttrInfoVO.getCommonAttrList().forEach(commonAttrVO -> commonAttrVO.setIsEdit("F"));
        }
        return defCommonAttrInfoVO;
    }
}
